package com.cogwheel.framework.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.cogwheel.framework.util.CDebug;

public class CMySQL {
	private static Connection connection = null;
	private static Statement statement = null;
	private static ResultSet resultset = null;
	private static String url = "jdbc:mysql://localhost:3306/testdb";
	private static String user = "root";
	private static String password = "root";
	
	static{ }

	public static boolean CWGLogin(String username, String passMD5hash){
		try{
			resultset = statement.executeQuery("SELECT * FROM users WHERE name=\""+ username +"\"");
        	while(resultset.next()){
        		// TODO: MD5-based password check...!
        		if (username.equals(resultset.getString(1))){
        			return true;
        		}
        	}
		}catch (Exception e){}
		
		return false;
	}
	
	public static void CWGConnect(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(url, user, password);
			statement = connection.createStatement();
            
			resultset = statement.executeQuery("SELECT VERSION()");
            if (resultset.next()) {
                System.out.println("Succesfully connected to MySQL server: " + resultset.getString(1));
            }
		}
		catch (Exception e){ CDebug.info("ICMySQL", "Error: " + e.getMessage()); }
	}
}
